$(function () {
    // wzERP.Contract.init();
    // wzERP.Contract.turnPage();//分页
    wzERP.Common.inlineDate('#filingDate1', '#filingDate2');  //联动日期
});
//时间过滤方法
template.defaults.imports.dateTimes=function(now){
    if(now==null){
        return ''
    }
    function add0(m){return m<10?'0'+m:m }
    var time = new Date(now);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
//处理数据方法
template.defaults.imports.dateCantent=function(cantent){
    var dateCantent = JSON.parse(cantent)
    var content=[]
    var contents
    for (var i=0;i<dateCantent.length;i++){
        contents = dateCantent[i].id+'-'+dateCantent[i].name
        content.push(contents)
    }
    return content.join(';')
}
// 商品服务显示
template.defaults.imports.formatInternationDetail=function(content){
    content = JSON.parse(content)
    var temp = '';
    for(var i = 0; i< content.length; i++) {
        temp += content[i].midtypeid + '-' + content[i].smalltypeid + '-' + content[i].smalltypename;
    }
    return temp;
}
//处理审批结果方法
template.defaults.imports.approvalResult=function(cantent){
    var tt = cantent.replace('\t','\\\\t')
    var dateCantent = JSON.parse(tt)
    return dateCantent.pathName

}
//获取日志备注方法
template.defaults.imports.remarksS=function(cantent){
    var tt = cantent.replace('\t','\\\\t')
    var dateCantent = JSON.parse(tt)
    return dateCantent.mark

}
//处理图片缓存问题
template.defaults.imports.remarksData=function(cantent){
    var data = Date.parse(new Date())
    return  cantent +'&'+ data

}
//获取时间和内容方法和当前页，每页显示条数，客户ID
function timeS() {
    var timeA={}
    // 获取表单时间
    var content = $("#filingSearchForm").serialize()
    //使用方法分割取出
    var contentS =content.split('&')
    var obj = {}
    for(var i = 0;i<contentS.length;i++){
        var con = contentS[i].toString()
        var son = con.split('=')
        obj[son[0]]=son[1]
    }
    //获取当前每页应该显示条数
    timeA.num = $('#filingLinenumberOf select').val()
    //获取搜索框内容
    timeA.searchKey = $('#filingSearchKeyA').val()
    //获取分割好的起止时间
    timeA.startDate = obj.startDate
    timeA.endDate = obj.endDate
    //当前页
    timeA.page =$('.layui-laypage-skip').val()
    return timeA
}
//用户信息
var account_info;
//刷新当前页方法
function queryS(curr,num,searchKey,startDate,endDate,name){
    //curr:当前页
    //num:每页显示条数
    //searchKey:搜索内容
    //startDate:开始时间
    //endDate:结束时间
    //name客户ID
    $.get('/filing/manage/queryByPage',{current:curr || 1,pageSize:num || 10,searchKey:searchKey,startDate:startDate,endDate:endDate,custId:name},function(data){
        //获取内容显示到模板上
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        var content = data
        //获取当前登录人信息
        $.get('/account/queryAccountDetail',function(data){
          
            for(var i =  0;i<content.data.records.length;i++){
                content.data.records[i].companyCodes=data.data.companyCode
            }
            //获取的内容加载到页面
            $('#filingListPlacement').html(template('filingList',content.data))
            //渲染页数
            $('.filingRecordOne').text(content.data.total)
            //渲染条数
            $('.filingRecordTwo').text(content.data.pages)
            permissonHandler();
            console.log(content)
            // 分页
            layui.use(['laypage', 'layer'], function () {
                var laypage = layui.laypage;
                laypage({
                    cont: 'turn-page',
                    curr: curr || 1,
                    pages: content.data.pages,
                    skin: '#457fe6',
                    skip: true,
                    jump:function (obj,first) {
                        if(!first){
                            $.get('/customer/queryKVList',function(data){
                                if (data.code==9999)
                                {
                                    layer.msg(data.msg,{time: 1000});
                                    wzERP.Common.TimeoutLogin();
                                    return false;
                                }
                                if($('#filingCustomerContent').val()!=''){
                                    var val
                                    var name=$('#filingCustomerContent').val()
                                    for(var i=0;i<data.data.length;i++){
                                        if(data.data[i].value==name){
                                            val = data.data[i].key
                                        }
                                    }
                                }else {
                                    var val = null
                                }
                                var timeA = timeS()
                                //获取当前每页应该显示条数
                                queryS(obj.curr,timeA.num,timeA.searchKey,timeA.startDate,timeA.endDate,val)
                            })
                        }
                    }
                });
            });
        })
    })
}
//渲染搜索客户
$('#filingCustomerSearch').on('keyup',function(){
    $.get('/customer/queryKVList',function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        console.log(data)
        var content = [
        ]
        var description=[]
        for(var i = 0 ;i<data.data.length;i++){
            content.push({title:data.data[i].value})
        }
        console.log(description)
        $('#filingCustomerSearch').search({
            source: content,
        })
    })
})
// 进入渲染当前页面
query(1)
//全局点击事件节流
var throttle = true
function query(curr,num){
    //curr:当前页
    //num:每页显示条数
    $.get('/filing/manage/queryByPage',{current:curr || 1,pageSize:num || 10},function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        //获取内容显示到模板上
        var content = data
        //获取当前登录人信息
        $.get('/account/queryAccountDetail',function(data){
            permissonHandler();
            account_info = data.data;
            for(var i =  0;i<content.data.records.length;i++){
                content.data.records[i].companyCodes=data.data.companyCode
            }
            //获取的内容加载到页面
            $('#filingListPlacement').html(template('filingList',content.data))
            //渲染页数
            $('.filingRecordOne').text(content.data.total)
            //渲染条数
            $('.filingRecordTwo').text(content.data.pages)
            permissonHandler();
            console.log(content)
            // 分页
            layui.use(['laypage', 'layer'], function () {
                var laypage = layui.laypage;
                laypage({
                    cont: 'turn-page',
                    curr: curr || 1,
                    pages: content.data.pages,
                    skin: '#457fe6',
                    skip: true,
                    jump:function (obj,first) {
                        //获取当前每页应该显示条数
                        var num = $('#filingLinenumberOf select').val()
                        if(!first){
                            //当前页
                            query(obj.curr,num)
                        }
                    }
                });
            });
        })
    })
}
// 每页显示条数
$('#filingLinenumberOf').on('click',function(){
    //获取客户ID
    $.get('/customer/queryKVList',function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        if($('#filingCustomerContent').val()!=''){
            var val
            var name=$('#filingCustomerContent').val()
            for(var i=0;i<data.data.length;i++){
                if(data.data[i].value==name){
                    val = data.data[i].key
                }
            }
        }else {
            var val = null
        }
        var timeA = timeS()
        //获取当前每页应该显示条数
        queryS(timeA.page,timeA.num,timeA.searchKey,timeA.startDate,timeA.endDate,val)
    })
})
//查询业务
$("#filingTimeA").on('click',function(){
    //获取客户ID
    $.get('/customer/queryKVList',function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        if($('#filingCustomerContent').val()!=''){
            var val
            var name=$('#filingCustomerContent').val()
            for(var i=0;i<data.data.length;i++){
                if(data.data[i].value==name){
                    val = data.data[i].key
                }
            }
            if(val==undefined){
                layer.msg('客户不存在', {icon: 5})
            }
        }else {
            var val = null
        }
        var timeA = timeS()
        //获取当前每页应该显示条数
        queryS(1,timeA.num,timeA.searchKey,timeA.startDate,timeA.endDate,val)
    })
    return false
})
//详情功能
$('#filingListPlacement').on('click','.filingDetails',function(){
    $('#filingToModal').modal({
        closable: false,
        transition: 'fade'
    }).modal('show')
    var id = $(this).attr('data-id')
    $.get('/filing/manage/filingDetail',{id:id},function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        if(data.code==-1){
            layer.msg(data.msg, {icon: 5})
            return false
        }
        console.log(data);
        //立案信息
        $('#filingPlacementArea').html(template('filingDetailsTemplate',data.data))
        //操作日志
        $('#filingRightPicture').html(template('filingRightPictureTemplate',data.data))
        //流程图
        $('#filingRightPictureImg').html(template('filingImgTemplate',data.data))
    })
})
//编辑
$('#main').on('click','.case_edit_btn',function () {
    var option = {
        id: $(this).data('id'),
        orderCode: $(this).data('code'),
        _this: $(this),
        orderType: $(this).data('ordertype')-0,
        pageSort: 'la',
    };
    case_build.editPop(option);
});
//办理功能
$('#filingListPlacement').on('click','.filinHandle',function(){
    //清理遗留备注
    $('.filingTextarea').val('')
    $('#filingToExamineModal').modal({
        closable: false,
        transition: 'fade'
    }).modal('show')
    var id = $(this).attr('data-id')
    $('.filingAuditButton').attr('data-id',id)
    $.get('/filing/manage/filingDetail',{id:id},function(data){
        if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
        if(data.code==-1){
            layer.msg(data.msg, {icon: 5})
            return false
        }
        $('#filingAuditDetailsTop').html(template('filingDetailsTemplate',data.data))
        $('#filingAuditDetailsLower').html(template('filingRightPictureTemplate',data.data))
        $('#filingAuditPicture').html(template('filingImgTemplate',data.data))
        $.get('/filing/manage/nextNodeInfo',{id:id},function(data){
            $('.filingAuditButtonContent').html(template('filingAuditButtonTemplate',data.data))
        })
    })
})
//提交办理
$('.filingAuditButton').on('click',function () {
    //判断点击事件的次数
    if(!throttle){
        return false
    }
    if($('.filingAuditButtonContent').val()==''||$('.filingAuditButtonContent').val()==null){
        layer.msg('请选择审批人', {icon: 5})
        return false
    }
    var id = $(this).attr('data-id')
    var activityId=$('.filingAuditButtonContent').val()
    var pathName=$('.filingAuditButtonContent').find("option:selected").text()
    var mark=$('.filingTextarea').val()
    //防止事件点击2次
    throttle = false
    window.setTimeout(function () {
        throttle = true
    },2000)
    NetUtis.doPostBodySimple("/filing/manage/approve", {id:id,activityId:activityId,pathName:pathName,mark:mark},function(data){
        if(data.code==0){
            layer.msg('提交成功', {icon: 7})
            $('#filingToExamineModal').modal('hide')
            $.get('/customer/queryKVList',function(data){
                if($('#filingCustomerContent').val()!=''){
                    var val
                    var name=$('#filingCustomerContent').val()
                    for(var i=0;i<data.data.length;i++){
                        if(data.data[i].value==name){
                            val = data.data[i].key
                        }
                    }
                }else {
                    var val = null
                }
                var timeA = timeS()
                //获取当前每页应该显示条数
                queryS(timeA.page,timeA.num,timeA.searchKey,timeA.startDate,timeA.endDate,val)
            })
         }
         else if (data.code==9999)
        {
            layer.msg(data.msg,{time: 1000});
            wzERP.Common.TimeoutLogin();
            return false;
        }
         else{
            layer.msg(data.msg, {icon: 7})
        }
        },
    true,true)
})
//删除功能
$('#filingListPlacement').on('click','.filinDelete',function() {
    var id = $(this).data('id');
    layer.alert('确定删除该立案吗？', {
            title: '提示信息',
            btn: ['取消', '确认'],
            btn1:function(index){
                layer.close(index)
            },
            btn2:function(){
                $.get('/filing/manage/deleteFiling',{ id:id },function(data){
                    if(data.code == 0){
                        layer.msg('删除成功', {icon: 3})
                        $.get('/customer/queryKVList',function(data){
                            if($('#filingCustomerContent').val()!=''){
                                var val
                                var name=$('#filingCustomerContent').val()
                                for(var i=0;i<data.data.length;i++){
                                    if(data.data[i].value==name){
                                        val = data.data[i].key
                                    }
                                }
                            }else {
                                var val = null
                            }
                            var timeA = timeS()
                            //获取当前每页应该显示条数
                            queryS(timeA.page,timeA.num,timeA.searchKey,timeA.startDate,timeA.endDate,val)
                        })
                    }
                    else if (data.code==9999)
                    {
                        layer.msg(data.msg,{time: 1000});
                        wzERP.Common.TimeoutLogin();
                        return false;
                    }
                    else {
                        layer.msg('删除失败', {icon: 3})
                    }
                })
            }
        }
    );
})

